﻿using Abp.Auditing;
using Abp.Dependency;
using NLog;
using System.Threading.Tasks;
using ILogger = Castle.Core.Logging.ILogger;

namespace Basefr.Logging.Auditings
{
    /// <summary>
    /// 自定义的AuditingStore，使用日志框架记录AuditLog
    /// </summary>
    public class AuditingStore : IAuditingStore, ITransientDependency
    {
        private readonly ILogger _logger;

        public AuditingStore(ILogger logger)
        {
            _logger = logger;
        }

        public async Task SaveAsync(AuditInfo auditInfo)
        {
            await Task.Run(() =>
            {
                _logger.LogAudit(auditInfo, auditInfo.Exception != null ? LogLevel.Error : LogLevel.Info);
            });
        }

        public void Save(AuditInfo auditInfo)
        {
            _logger.LogAudit(auditInfo, auditInfo.Exception != null ? LogLevel.Error : LogLevel.Info);
        }
    }
}
